import {createRouter, createWebHistory} from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from "../views/Register.vue";
import store from "../store"

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        meta: {
            requireAuth: true
        },
        children: [
            {
                path: "/home",
                name: "HomeIndex",
                component: () => import('../components/HomeIndex'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: "/note",
                name: "HomeNote",
                component: () => import('../components/HomeNote'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: "/note/editor",
                name: "NoteEditor",
                component: () => import('../components/NoteEditor'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: "/note/details",
                name: "NoteDetails",
                component: () => import('../components/NoteDetails'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: "/library",
                name: "HomeLibrary",
                component: () => import('../components/HomeLibrary'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: "/config",
                name: "HomeConfig",
                component: () => import('../components/HomeConfig'),
                meta: {
                    requireAuth: true
                },
                children: [
                    {
                        path: "/config/user",
                        name: "UserDetails",
                        component: () => import('../components/UserDetails'),
                        meta: {
                            requireAuth: true
                        }
                    },
                    {
                        path: "/config/role",
                        name: "RoleDetails",
                        component: () => import('../components/RoleDetails'),
                        meta: {
                            requireAuth: true
                        }
                    },
                    {
                        path: "/config/permission",
                        name: "PermissionDetails",
                        component: () => import('../components/PermissionDetails'),
                        meta: {
                            requireAuth: true
                        }
                    },

                ]
            },
        ]
    },
    {
        path: '/about',
        name: 'About',
        component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})
let axios = require('axios')
router.beforeEach((to, from, next) => {
    //判断是否需要认证
    if (to.meta.requireAuth) {
        // //是否登录过
        // if (Object.keys(store.state.user).length !== 0) {
        //   next()
        // } else {
        //   next({
        //     path: 'login'
        //   })
        // }
        axios.get("test")
            .then(success => {
                if (success.data.code === 200) {
                    next()
                } else if (success.data.code === 300) {
                    store.commit("logout")
                    next(
                        {
                            path: '/login',
                            query:{redirect: to.fullPath}
                        }
                    )
                }
            }).catch(fail => {
            console.log(fail);
        })
    } else {
        next()
    }
})

export default router
